﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using Twipler.Xtensions;
using Twipler.App.Parse.Code;


namespace Twipler.App.Machine.Instructions
{
    [InstructionAttibute("Call Entity Method")]
    public class Call : Instruction
    {
        //private readonly string _methodName;
        private readonly StaticMethodCall _call;

        public Call(StaticMethodCall call)
        {
			if (call == null)
				throw new ArgumentNullException("call");

            _call = call;
        }

		public override string ToString()
		{
			return "call " + _call.ToString(true);
		}

        public override void Exec(Environment env)
        {
            env.Stack.Push(_call.Invoke(env));
            return;
        

			/*
            string className = _methodName.Substring(0, _methodName.IndexOf('.'));
            string methodName = _methodName.Substring(className.Length+1);
            
            Type entityType = Assembly.GetExecutingAssembly().GetType("Twipler.App.Entity." + className);
            if (entityType != null)
            {
                MethodInfo mi = entityType.GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static);

                if (mi != null)
                {
                    ParameterInfo[] param = mi.GetParameters();

                    if (env.Stack.Count() < param.Length)
                        return;

                    object[] things = new object[param.Length];
                    for (int i = 0; i < things.Length; i++)
                        things[things.Length - i - 1] = env.Stack.Pop();

                    object res = mi.Invoke(null, things);

                    env.Stack.Push(res);
                }

            }*/
        } 
    }
}